package com.lottery.ruok.lottrey.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.TraditionBuyAdapter;
import com.lottery.ruok.lottrey.bean.LotteryItem;
import com.lottery.ruok.lottrey.eventbusObject.AddBetSuccess;
import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.CreateOrderRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.CreateOrderResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.IToastAddBet;
import ui.NavigateBar;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/2.
 */

public class TraditionBuyDialog extends BaseFragment {

    @BindView(R.id.navigateBar)
    NavigateBar navigateBar;
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.confirm)
    TextView confirm;
    @BindView(R.id.delete)
    TextView delete;
    @BindView(R.id.money)
    TextView money;


    private ArrayList<LotteryItem> lotterys;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //自定义对话框样式
        View v = inflater.inflate(R.layout.tradition_buy_layout, null);

        lotterys = getArguments().getParcelableArrayList("lotterys");

        ButterKnife.bind(this, v);

        navigateBar.setOnIconClickListener(new NavigateBar.OnIconClickListener() {
            @Override
            public void OnIconClick(View view) {
                getFragmentManager().popBackStackImmediate();
            }
        });

        setMoney();

        return v;
    }

    @OnClick({R.id.confirm, R.id.delete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.confirm:
                if(lotterys.size() > 0) {
                    addBet();
                } else {
                    Utils.showToast("注数不能为空");
                }
                break;
            case R.id.delete:
                if(lotterys != null){
                    lotterys.clear();
                }

                setMoney();
                break;
        }
    }

    private void addBet(){
        JSONArray jsonArray = new JSONArray();
        for(LotteryItem lotteryItem : lotterys){
            JSONObject jsonObject = new JSONObject();
            try {
                jsonObject.put("rule_id", lotteryItem.rule_id);
                jsonObject.put("pick_number", lotteryItem.name);
                jsonObject.put("position", "");
                jsonObject.put("amount_per_note", lotteryItem.fillInString);
                jsonObject.put("note_count", 1);
                jsonObject.put("odds", lotteryItem.odd);
                if(lotteryItem.rakeback != null && !lotteryItem.rakeback.equals("")){
                    jsonObject.put("rakeback", lotteryItem.rakeback);
                } else {
                    jsonObject.put("rakeback", "0");
                }
                jsonObject.put("is_follow", 0);
                jsonObject.put("is_win_stop", 0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            jsonArray.put(jsonObject);
        }
        RetrofitBase.getRetrofit().create(CreateOrderRequest.class).getResult(jsonArray, UserInfo.getToken(), null)
                .compose(RxHelper.<CreateOrderResponse>handleResult())
                .subscribe(new RxSubscribe<CreateOrderResponse>(getActivity()) {
                    @Override
                    protected void _onNext(CreateOrderResponse createOrderResponse) {
                        IToastAddBet.show(getActivity().getApplicationContext());
                        Utils.showToast("余额为" + createOrderResponse.getAfter_amount() + "元");

//                        getActivity().finish();
                        getFragmentManager().popBackStackImmediate();

                        AddBetSuccess addBetSuccess = new AddBetSuccess();
                        addBetSuccess.setAfter_amount(createOrderResponse.getAfter_amount());
                        EventBus.getDefault().post(addBetSuccess);
                    }

                    @Override
                    public boolean showDialog() {
                        return true;
                    }
                });
    }

    public void setMoney(){
        int sum = 0;
        for(LotteryItem lotteryItem : lotterys){
            int number;
            try {
                number = Integer.parseInt(lotteryItem.fillInString);
            } catch (Exception e) {
                number = 0;
                e.printStackTrace();
            }
            sum+=number;
        }
        money.setText(sum + "元");

        if(listview.getAdapter() == null) {
            listview.setAdapter(new TraditionBuyAdapter(lotterys, this));
        } else {
            ((TraditionBuyAdapter)listview.getAdapter()).notifyDataSetChanged();
        }
    }
}
